﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Web.UI;
using System.Text;
using System.IO;

namespace TSOL.Helpers
{
    public static class ClassExtensions
    {
        public static string RemoveNull(this string svalue)
        {
            if (string.IsNullOrEmpty(svalue))
                svalue = string.Empty;

            return svalue;
        }

        public static T With<T>(this T withItem, Action<T> withAction)
        {
            withAction(withItem);
            return withItem;
        }

        public static Control AddChilds(this Control ParentControl, Action<ControlCollection> Proc)
        {
            Proc(ParentControl.Controls);
            return ParentControl;
        }
        public static string RenderToString(this Control ParentControl)
        {
            TextWriter _parentControlTextWriter = new StringWriter();
            HtmlTextWriter _parentControlWriter = new HtmlTextWriter(_parentControlTextWriter);
            ParentControl.RenderControl(_parentControlWriter);
            return _parentControlTextWriter.ToString();
        }

        public static void usingWeb(this SPWeb ParentWeb, string WebName, Action<SPWeb> Proc)
        {
            using (SPWeb nWeb = ParentWeb.Webs.AsSafeEnumerable().Where(x => x.Name == WebName).SingleOrDefault())
            {
                Proc(nWeb);
            }
        }

        public static void safeUsingWeb(this SPSite Site, Action<SPSite, SPWeb> Proc)
        {
            try
            {
                using (SPWeb nWeb = Site.OpenWeb())
                {
                    Proc(Site, nWeb);
                }
            }
            finally
            {
                Site.Dispose();
            }
        }

        public static void safeUsingWeb(this SPWeb Web, string WebName, Action<SPWeb> Proc)
        {
            using (SPWeb nWeb = Web.Webs[WebName])
            {
                Proc(nWeb);
            }
        }

        public static void safeUsingRootWeb(this SPSite Site, string WebName, Action<SPWeb> Proc)
        {
            using (SPWeb nWeb = Site.RootWeb.Webs[WebName])
            {
                Proc(nWeb);
            }
        }
        public static void safeUsingSite(this SPSite Site, Action<SPSite> Proc)
        {
            try
            {
                Proc(Site);
            }
            finally
            {
                Site.Dispose();
            }
        }

        public static void safeUsingRootWeb(this SPSite Site, Action<SPSite, SPWeb> Proc)
        {
            try
            {
                using (SPWeb nWeb = Site.RootWeb)
                {
                    Proc(Site, nWeb);
                }
            }
            finally
            {
                Site.Dispose();
            }
        }


        public static IEnumerable<SPWeb> AsSafeEnumerable(this SPWebCollection webs)
        {
            foreach (SPWeb web in webs)
            {
                try
                {
                    yield return web;
                }
                finally
                {
                    web.Dispose();
                }
            }
        }

        public static IEnumerable<SPSite> AsSafeEnumerable(this SPSiteCollection sites)
        {
            foreach (SPSite site in sites)
            {
                try
                {
                    yield return site;
                }
                finally
                {
                    site.Dispose();
                }
            }
        }

        public static SPList safeGetList(this SPWeb web, string listUrl)
        {
            try
            {
                return web.GetList(listUrl);
            }
            catch
            {
                return null;
            }
        }
        public static SPList safeLists(this SPWeb web, string listUrl)
        {
            try
            {
                return web.Lists[listUrl];
            }
            catch
            {
                return null;
            }
        }
        public static bool SafeExists(string Url)
        {
            SPSite _site = null;
            SPWeb _web = null;
            bool siteExists = false;

            try
            {
                _site = new SPSite(Url);
                _web = _site.OpenWeb();
                siteExists = true;
            }
            catch (Exception) { }
            finally
            {
                if (_site != null) _site.Dispose();
                if (_web != null) _web.Dispose();
            }

            return siteExists;
        }
    }
}